package com.lzs.adx.web.modules.adx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzs.adx.commmon.utils.DateUtils;
import com.lzs.adx.web.modules.adx.dao.LoanReportDayDao;
import com.lzs.adx.web.modules.adx.dto.LoanReportDayDTO;
import com.lzs.adx.web.modules.adx.dto.ReportExcelDTO;
import com.lzs.adx.web.modules.adx.entity.AccountReportDayEntity;
import com.lzs.adx.web.modules.adx.entity.LoanReportDayEntity;
import com.lzs.adx.web.modules.adx.service.LoanReportDayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账户日结算表
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2024-09-04
 */
@Service
public class LoanReportDayServiceImpl extends CrudServiceImpl<LoanReportDayDao, LoanReportDayEntity, LoanReportDayDTO> implements LoanReportDayService {

    @Override
    public QueryWrapper<LoanReportDayEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String business = (String)params.get("business");
        String productName = (String)params.get("productName");
        String stime = (String)params.get("stime");
        String etime = (String)params.get("etime");

        QueryWrapper<LoanReportDayEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(productName), "product_name", productName);
        wrapper.eq(StringUtils.isNotBlank(business), "business", business);
        wrapper.ge(StringUtils.isNotBlank(stime), "day", stime);
        wrapper.le(StringUtils.isNotBlank(etime), "day", etime);

        return wrapper;
    }


    @Override
    public void generateData(ReportExcelDTO reportExcelDTO) {
        //生成报表
        LambdaQueryWrapper<LoanReportDayEntity> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(LoanReportDayEntity::getDay,reportExcelDTO.getDay());
        lambdaQueryWrapper.eq(LoanReportDayEntity::getProductName,reportExcelDTO.getProductName());
        List<LoanReportDayEntity> loanReportDayEntities = baseDao.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(loanReportDayEntities)){
            LoanReportDayEntity dbLoanReportDay = loanReportDayEntities.get(0);
            LoanReportDayEntity newValue = new LoanReportDayEntity();
            BeanUtils.copyProperties(reportExcelDTO,newValue);
            newValue.setMonth(DateUtils.getMonth(reportExcelDTO.getDay()));
            newValue.setUpdateTime(new Date());
            newValue.setId(dbLoanReportDay.getId());
            baseDao.updateById(newValue);
        }else{
            LoanReportDayEntity newValue = new LoanReportDayEntity();
            BeanUtils.copyProperties(reportExcelDTO,newValue);
            newValue.setMonth(DateUtils.getMonth(reportExcelDTO.getDay()));
            newValue.setCreateTime(new Date());
            newValue.setUpdateTime(new Date());
            newValue.setActEventTypeTotal(reportExcelDTO.getActEventTypeTotal());
            baseDao.insert(newValue);
        }
    }
}